from datetime import date

from PySide.QtCore import *
from PySide.QtGui import *
from PySide.QtSql import *

from ui_elements.task import Ui_TaskDialog


class TaskDialog(QDialog, Ui_TaskDialog):
    
    REASON_ADD = 0
    REASON_EDIT = 1
    
    def __init__(self, icon, *args, **kwargs):
        QDialog.__init__(self, *args, **kwargs)
        self.setWindowFlags(Qt.CustomizeWindowHint)
        self.setWindowIcon(icon)
        self.setup()
        self.accepted.connect(self.do_accepted)
        self.rejected.connect(self.do_rejected)
        self.open_reason = -1
        self.id = -1
        
    def setup(self):
        self.setupUi(self)
        
    def show(self, reason=None, id=None):
        if reason == self.REASON_ADD:
            self.open_reason = self.REASON_ADD
            self.setWindowTitle('Add task')
        elif reason == self.REASON_EDIT:
            self.open_reason = self.REASON_EDIT
            self.setWindowTitle('Edit task')
            
        self.date_display.setText(str(date.today()))
        super(TaskDialog, self).show()
        
    def showEvent(self, event):
        g = self.geometry()
        p = QCursor.pos()
        self.setGeometry(p.x()-(g.width()/2), p.y()-(g.height()/2), g.width(), g.height())
        super(TaskDialog, self).showEvent(event)
        
    def clear(self):
        self.category_combobox.setCurrentIndex(-1)
        self.task_entry.setText('')
        
    def do_accepted(self):
        print "accepted"
        q = QSqlQuery()
        if self.open_reason == self.REASON_ADD:
            pass
        elif self.open_reason == self.REASON_EDIT:
            pass
        
    def do_rejected(self):
        print "rejected"
        self.clear()